<?php
/* Import Subject controller to get list of subjects */
App::import('Controller', 'Subjects');
 
class SchedulesController extends AppController {
	public $helpers = array('Html', 'Form', 'Session');
	
	function schedule_index($subject_id = NULL){
		/* Create Subject controller to get all subjects */
		$subjects = new SubjectsController;
		
		/* Load model, components... */
		$subjects->constructClasses();
		
		$this->set('subjects', $subjects->Subject->find('all'));
		$this->set('subject_id', $subject_id);
		$this->set('weekdays', $this->Schedule->weekdays);
		
		if ($this->request->is('post')) {
			$this->redirect(array('action' => 'schedule_add', $subject_id));
		}
	}
	
	/* Request action to get schedules by subject ID */
	function schedulesFromSubIdGet(){
		if (empty($this->request->params['requested'])) {
			throw new ForbiddenException();
		}
		
		$subject_id = $this->request->params['subject_id'];
		
		/* Find schedule by subject id, one subject may have many schedules */
		return $schedules = $this->Schedule->findAllBym_sdl_sub_id($subject_id);
	}
	
	/* Add schedule for a subject by subject id*/
	public function schedule_add($subject_id = null){
		if (!$subject_id) {
			throw new NotFoundException(__('Invalid post'));
		}
		
		$this->set('weekdays', $this->Schedule->weekdays);
		
		if ($this->request->is('post')) {
			/* Create new schedule */
			$this->Schedule->create();
			
			/* Overwrite subject Id */
			$this->request->data['Schedule']['m_sdl_sub_id'] = $subject_id;
			
			if ($this->Schedule->save($this->request->data)) {
				$this->Session->setFlash('Your schedule has been saved');
				$this->redirect(array('action' => 'schedule_index', $subject_id));
			} else {
				$this->Session->setFlash('Unable to add your schedule.');
			}
		}
	}
	
	/* Edit subject's schedule, input is subject id */
	public function schedule_edit($subject_id, $schedule_id = null) {
		if (!$schedule_id) {
			throw new NotFoundException(__('Invalid post'));
		}
		
		$this->set('weekdays', $this->Schedule->weekdays);
		$schedule = $this->Schedule->findBym_sdl_id($schedule_id);
		
		if (!$schedule) {
			throw new NotFoundException(__('Invalid post'));
		}
		
		if ($this->request->is('post') || $this->request->is('put')) {
			
			$dataNeedToSave = array('Schedule.m_sdl_sub_id' => $subject_id,
					                'Schedule.m_sdl_week_day' => $this->request->data['Schedule']['m_sdl_week_day'],
					                'Schedule.m_sdl_start_hour' => $this->request->data['Schedule']['m_sdl_start_hour'],
					                'Schedule.m_sdl_end_hour' => $this->request->data['Schedule']['m_sdl_end_hour']);
			
			$saveCondition = array('Schedule.m_sdl_id' => $schedule_id);
			
			if ($this->Schedule->updateAll($dataNeedToSave, $saveCondition)) {
				$this->Session->setFlash('Schedule '. $schedule_id .' has been updated.');
				$this->redirect(array('action' => 'schedule_index', $subject_id));
			} else {
				$this->Session->setFlash('Unable to update schedule.');
			}
		}
		
		if (!$this->request->data) {
			$this->request->data = $schedule;
		}
	}
	
	public function schedule_delete($subject_id, $schedule_id = NULL){
		if ($this->request->is('get')) {
			throw new MethodNotAllowedException();
		}
		
		if ($this->Schedule->deleteAll(array('Schedule.m_sdl_id' => $schedule_id), false)) {
			$this->Session->setFlash('The schedule with id: ' . $schedule_id . ' has been deleted.');
			$this->redirect(array('action' => 'schedule_index', $subject_id));
		}
	}
	
}
?>